package com.uum.common.core.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import jdk.nashorn.internal.ir.CallNode;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Tsang_yc
 **/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {

    private String code;

    private T data;

    private String msg;

    private Boolean status;

    private Integer total;


    public Result add(String key, Object value) {
        Map<String, Object> mapData = new HashMap();
        mapData.put(key, value);
        return getResult(mapData);
    }

    public Result add(Map map) {
        return getResult(map);
    }

    private Result getResult(Map mapData) {
        T data = this.getData();
        if (data instanceof Map) {
            Map t = (Map) data;
            t.putAll(mapData);
            this.setData((T) t);
        } else {
            this.setData((T) mapData);
        }
        return this;
    }

    public static Result success() {
        return success(null);
    }

    public static Result success(String msg) {
        return result(ResultCode.SUCCESS.getCode(), msg, null, true);
    }

    public static <T> Result<T> success(T data) {
        ResultCode rce = ResultCode.SUCCESS;
        if (data instanceof Boolean && Boolean.FALSE.equals(data)) {
            rce = ResultCode.SYSTEM_EXECUTION_ERROR;
        }
        return result(rce, data, true);
    }

    public static <T> Result<T> success(T data, Long total) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(data);
        result.setTotal(total.intValue());
        return result;
    }


    public static Result failed() {
        return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), ResultCode.SYSTEM_EXECUTION_ERROR.getMsg(), null, false);
    }

    public static Result failed(ResultCode resultCode) {
        return result(resultCode.getCode(), resultCode.getMsg(), null, false);
    }

    public static Result failed(String msg) {
        return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), msg, null, false);
    }

    public static Result failed(String code, String msg) {
        return result(code, msg, null, false);
    }

    public static Result failed(IResultCode resultCode) {
        return result(resultCode.getCode(), resultCode.getMsg(), null, false);
    }

    public static <T> Result<T> failed(IResultCode resultCode, T data) {
        return result(resultCode.getCode(), resultCode.getMsg(), data, false);
    }

    public static Result failed(IResultCode resultCode, String msg) {
        return result(resultCode.getCode(), msg, null, false);
    }

    public static Result judge(boolean status) {
        if (status) {
            return success();
        } else {
            return failed();
        }
    }

    public static Result judge(boolean status, String successMsg, String failMsg) {
        if (status) {
            return success(successMsg);
        } else {
            return failed(failMsg);
        }
    }

    private static <T> Result<T> result(IResultCode resultCode, T data, Boolean status) {
        return result(resultCode.getCode(), resultCode.getMsg(), data, status);
    }

    private static <T> Result<T> result(String code, String msg, T data, Boolean status) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        result.setStatus(status);
        return result;
    }


    public static boolean isSuccess(Result<?> result) {
        return result != null && result.status && ResultCode.SUCCESS.getCode().equals(result.getCode());
    }
}
